package com.epam.cdp.jpa.services;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.epam.cdp.jpa.dao.UserDAO;
import com.epam.cdp.jpa.model.User;

@Stateless
public class LoginService {

    @EJB
    private UserDAO userDAO;
    
    public boolean areCredentialsValid(String username, String password) {
	boolean result = false;

	if (notEmpty(username, password)) {
	    User user = userDAO.getByEmail(username);
	    if (user != null) {
		result = password.equals(user.getPassword());
	    }
	}
	
	return result;
    }

    public User getUserByCredentials(String username, String password) {
	User result = null;
	
	if (notEmpty(username, password)) {
	    User user = userDAO.getByEmail(username);
	    if (user != null && password.equals(user.getPassword())) {
		result = user;
	    }
	}
	
	return result;
    }
    
    
    private boolean notEmpty(String username, String password) {
	return 	username != null &&
		password != null && 
		!username.trim().isEmpty() &&
		!password.trim().isEmpty();
    }
}
